/**
 * @param {number} n
 * @return {string}
 */
var countAndSay = function(n) {
  const str = new Array(30).fill("");
  str[0] = "1", str[1] = "11";
  function countNumNext(lastStr) {
    let nowNum = lastStr[0], count = 1, result = "";
    for(let i = 1; i < lastStr.length; i++) {
      if(lastStr[i] == nowNum) {
        count++;
        if(i == lastStr.length - 1) {
          result += (count + lastStr[i]) + "";
        }
      } else {
        if(i == lastStr.length - 1) {
          result += (count + lastStr[i - 1]);
          count = 1;
          result += (count + lastStr[i]) + "";
          break;
        }
        result += (count + lastStr[i - 1]) + "";
        count = 1;
        nowNum = lastStr[i];

      }
    }
    return result;
  }
  for(let i = 2; i < 30; i++) {
    str[i] = countNumNext(str[i - 1]);
  }
  return str[n - 1];
};
let n = 1;
console.log(countAndSay(n));
n = 3;
console.log(countAndSay(n));
n = 4;
console.log(countAndSay(n));
n = 5;
console.log(countAndSay(n));
